<?php

declare (strict_types=1);


namespace app\api\controller;

use app\common\model\VerificationCode;
use app\common\tool\Result;

/**
 * 短信验证码
 */
class Captcha extends Base
{
    /**
     * 发送验证码
     */
    public function send()
    {
        $params = $this->request->param();
        if (empty($params['mobile'])) {
            if (!$this->verifyToken($this->request->header('token'))) {
                return Result::fail('请登录后再发送短信验证码');
            }
            $params['mobile'] = $this->user->mobile;
        }

        $result = VerificationCode::_create($params['mobile']);
        if ($result['code'] != 1) {
            return Result::fail($result['msg']);
        }
        return Result::success('发送成功');
    }
}